<?php

namespace app\admin\controller\crm;

use app\admin\controller\BaseController;
use app\admin\logic\crm\CrmFollowUpsLogic;
use app\admin\validate\crm\CrmFollowUpsValidate;
use support\Request;
use support\Response;

/**
 * 客户跟进记录控制器
 * 用于管理客户跟进记录
 */
class CrmFollowUpsController extends BaseController
{
    /**
     * @var CrmFollowUpsLogic
     */
    protected $logic;

    /**
     * @var CrmFollowUpsValidate
     */
    protected $validate;
    
    /**
     * 构造方法
     */
    public function __construct()
    {
        $this->logic = new CrmFollowUpsLogic();
        $this->validate = new CrmFollowUpsValidate();
        parent::__construct();
    }

    /**
     * 获取跟进记录列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request) : Response
{
    $where = $request->more([
        ['name', ''],
        ['user', ''],
        ['type', ''],
        ['result', ''],
        ['area_id', ''],
        ['create_time', []],
        ['followup_time', []],
    ]);
    
    $data = $this->logic->getFollowUpList($where);
    foreach ($data['data'] as &$item) {
        if (!empty($item['customer']['metas'])) {
            $metas = $item['customer']['metas'];
            $_tmp = [];
            foreach ($metas as $meta) {
                $_tmp[$meta['meta_key']] = $meta['meta_value'];
            }
            $item['customer']['metas'] = $_tmp;
        } else {
            $item['customer']['metas'] = [];
        }
    }
    return $this->success($data);
}

    /**
     * 保存跟进记录
     * @param Request $request
     * @return Response
     */
    public function save(Request $request) : Response
    {
        $data = $request->post();
        
        if (!$this->validate->check($data)) {
            return $this->fail($this->validate->getError());
        }

        $result = $this->logic->save($data);
        if ($result) {
            return $this->success($result, '保存成功');
        }
        
        return $this->fail('保存失败');
    }

    /**
     * 更新跟进记录
     * @param Request $request
     * @param mixed $id
     * @return Response
     */
    public function update(Request $request, $id) : Response
    {
        $id = $request->input('id', $id);
        if (empty($id)) {
            return $this->fail('参数错误');
        }

        $data = $request->post();
        
        if (!$this->validate->check($data)) {
            return $this->fail($this->validate->getError());
        }

        $result = $this->logic->update($id, $data);
        if ($result) {
            return $this->success($result, '更新成功');
        }
        
        return $this->fail('更新失败');
    }
    
    public function export(Request $request) : Response
    {
        $where = $request->more([
            ['name', ''],
            ['user', ''],
            ['type', ''],
            ['result', ''],
            ['area_id', ''],
            ['create_time', []],
        ]);
        
        return $this->logic->export($where);
    }
    
}